<?php
//------------------------------------------------------------------------------------------------------------------------------
//	Nom : lib_anote.php
//	Auteurs: Defondemieres Fabrice - Delfour Kevin
//	Description :
//------------------------------------------------------------------------------------------------------------------------------
 
//Gestion des erreurs
//ini_set("error_reporting", E_ALL);
//ini_set("display_errors","0"); 
//ini_set("ignore_repeated_errors","0");
//ini_set("log_errors", "0" ); 
//ini_set("error_log", "errors/errors_Lib_Anote.txt" ); 

//Fonction permettant de transformer un fichier contenant des notes en 
//tableau
function note2array ($target){
	$i=0;$j=0;
	$tampon='';
	$note2array = '';
	
	$fp =  fopen("data/note/$target.txt","r");
	while (!feof ($fp))
	{
		$tampon .= fgets($fp);
	}
	$fsplit = split (";;",$tampon);
	for ($i=0; $i !=sizeOf($fsplit)-1; $i++){
		list($note_split,$link_split) = split ("@@",$fsplit[$i]);
		list($num,$select,$note) = split ("::",$note_split);
		$note2array[$i][0] = $num; 		//Numero de la note
		$note2array[$i][1] = $select; 	//La selection
		$note2array[$i][2] = $note; 	//L'annotation
		$note2array[$i][3] = $link_split; //La liste des sources...
	}
		
	fclose($fp);
	return $note2array;
}

function read_art($target){
	$i=0;
	$tampon='';
	$fp =  fopen("data/art/$target.txt","r");
	while (!feof ($fp))
	{
		$tampon = fgets($fp);
		$tampon = nl2br($tampon);
		if ($tampon !=''){
			$article2array [$i] = $tampon;
			$i = $i + 1;
		}
		$tampon = '';
	}
	fclose($fp);
	$string ='';
	for ($i=0; $i != sizeOf($article2array ); $i++){
		$string .= $article2array [$i];
	}
	$string = stripslashes($string);
	$string = strip_tags($string);
	return $string;
}

function array2string ($array){
//a modifier si source ajouter @@ x::x::x
	$string ='';
	if (sizeOf($array) > 0) {
		for ($i=0; $i != sizeOf($array); $i++){
			$num = $array[$i][0];
			$selection = $array[$i][1];
			$annotation = $array[$i][2];
			$source = $array[$i][3];
			if (($selection!="")|($annotation!=""))
				$string .= "$num::$selection::$annotation@@$source;;";
			}
	}
	return $string;
}

function array2file ($array,$target){
	//on recupere le contenu du tableau
	$donnees = array2string ($array);
	$donnees = convertSymbolstoEntities($donnees);
	//echo $donnees;
	
	if (file_exists("data/note/$target.txt")) {
		unlink("data/note/$target.txt");
	}
	//On cr� le fichier avec le nouveau contenu
	$fp = fopen ("data/note/$target.txt", "w");
	//On pointe sur le debut du fichier
	fseek ($fp, 0);
	//On enregistre le contenu du fichier
	fputs ($fp, $donnees);
	//On ferme le fichier, et le tour est jou�
	fclose ($fp); 
}

function read_note ($target){
	$array = NULL;
	if (file_exists("data/note/$target.txt")) {
		$array = note2array($target);
		return $array;
	}
	else {
		return NULL;
	}
}

function write_note($target,$selection,$annotation,$source){
	$array = read_note($target);
	$string = read_art($target);
	
	$string = convertSymbolstoEntities($string);
	$selection = convertSymbolstoEntities($selection);
	$annotation = convertSymbolstoEntities($annotation);
	
	if(ereg($selection,$string)){
		if ($array != NULL){
			$taille = sizeOf($array);
			$array[$taille][0] = $taille;
			$array[$taille][1] = $selection;
			$array[$taille][2] = $annotation;
			$array[$taille][3] = $source;
		}
		else {
			$array[0][0] = 0;
			$array[0][1] = $selection;
			$array[0][2] = $annotation;
			$array[0][3] = "::";
		}
		array2file($array,$target);
		return 1;
	}
	else {
		return 0;
	}
}

function givemeline($array,$num){
	$num_ligne = -1;
	for ($i=0;$i != sizeOf($array); $i++){
		$temp = $array[$i][0];
		if ($array[$i][0] == $num){
			$num_ligne = $i;
			return $num_ligne;
		}
	}
	return $num_ligne;
}

function modify_note($target,$num,$selection,$annotation,$source){
	$array = read_note($target);
	$string = read_art($target);
	
	$string = convertSymbolstoEntities($string);
	$selection = convertSymbolstoEntities($selection);
	$annotation = convertSymbolstoEntities($annotation);
	
	$num_ligne = givemeline($array,$num);
	if ($num_ligne != -1){
		$array[$num_ligne][0] = $num;
		$array[$num_ligne][1] = $selection;
		$array[$num_ligne][2] = $annotation;
		$array[$num_ligne][3] = $source;
		
		array2file($array,$target);
		
		return 1;
	}
	else return 0;
}

function delete_note($target,$num){
	$array_f = null;
	$array = read_note($target);
	$num_ligne = givemeline($array,$num);
	$array[$num_ligne][0] = null;
	$array[$num_ligne][1] = null;
	$array[$num_ligne][2] = null;
	$array[$num_ligne][3] = null;
	
	if (sizeOf($array) == 1){
		unlink("data/note/$target.txt");
	}
	else{
		for ($i=0; $i != sizeOf($array); $i++){
			if ($num_ligne != $i) {
				$array_f[$i][0] = $array[$i][0];
				$array_f[$i][1] = $array[$i][1];
				$array_f[$i][2] = $array[$i][2];
				
				$string = $array[$i][3]; 
				if(ereg($num,$string)){
					$string = str_replace($num,"",$string);
				}
				$array_f[$i][3] = $string;  
			}
		}
		array2file($array_f,$target);
	}
	return 2;
}

function return_source($target,$num){
	$array_source = NULL;
	$array = read_note($target);
	$num_ligne = givemeline($array,$num);
	$string = $array[$num_ligne][3];
	if (strlen($string)>2){
		$array_source = split("::",$string);
	}
	return $array_source;
}

function array2checkboxchecked ($array, $array_checked){ 
	$string = NULL;
	$checkornot = NULL;
	if (sizeOf($array) > 0) {
		for ($i=0;$i !=sizeOf($array) ;$i++){
			$checked = -1;
			$num = $array[$i][0];
			$selection = $array[$i][1];
			$annotation = $array[$i][2];
			if ($array_checked != NULL){
				for($j=0;$j !=sizeOf($array_checked) ;$j++){
					if ($array_checked[$j] != NULL){
						$tmp = $array_checked[$j];
						if ($num == $tmp){
							$checked = 1;
						}
					}
				}
				
			}
			else {
				$checked = -1;
			}
			if ($checked > 0){
				$string .= "<INPUT type='checkbox' name='$i' value='$num' checked='true'><b>$selection</b> anoted with <i>\"$annotation\"</i></input><br/>";
			}
			else {
				$string .= "<INPUT type='checkbox' name='$i' value='$num'><b>$selection</b> anoted with <i>\"$annotation\"</i></input><br/>";
			}
		}
	}
	return $string;
}	

function array2checkbox ($array){
	$string='';
	if (sizeOf($array) > 0) {
		for ($i=0; $i != sizeOf($array); $i++){
			$num = $array[$i][0];
			$selection = $array[$i][1];
			$annotation = $array[$i][2];
			if (($selection!="")|($annotation!=""))
				$string .= "<INPUT type='checkbox' name='$i' value='$num'><b>$selection</b> anoted with <i>\"$annotation\"</i></input><br/>";
		}
	}
	return $string;
}

function validateaString($element, $require_content = true){
	return (!is_string($element)) ? false : ($require_content && $element == '' ? false : true);
}

function convertSymbolstoEntities($string){
	static $symbols = 
		array(
			'‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '‘', '’', '“', '”', 
			'•', '–', '—', '˜', '™', 'š', '›', 'œ', 'Ÿ', '€', 'Æ', 'Á', 'Â', 'À', 'Å', 
			'Ã', 'Ä', 'Ç', 'Ð', 'É', 'Ê', 'È', 'Ë', 'Í', 'Î', 'Ì', 'Ï', 'Ñ', 'Ó', 'Ô', 
			'Ò', 'Ø', 'Õ', 'Ö', 'Þ', 'Ú', 'Û', 'Ù', 'Ü', 'Ý', 'á', 'â', 'æ', 'à', 'å', 
			'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ð', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 
			'ò', 'ø', 'õ', 'ö', 'ß', 'þ', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', '¡', '£', '¤', 
			'¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', 
			'´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', '×', '÷', '¢', 
			'…', 'µ');
	static $entities = 
		array(
			'&#8218;',  '&#402;',   '&#8222;',  '&#8230;',  '&#8224;',  '&#8225;',  '&#710;', 
			'&#8240;',  '&#352;',   '&#8249;',  '&#338;',   '&#8216;',  '&#8217;',  '&#8220;', 
			'&#8221;',  '&#8226;',  '&#8211;',  '&#8212;',  '&#732;',   '&#8482;',  '&#353;', 
			'&#8250;',  '&#339;',   '&#376;',   '&#8364;',  '&aelig;',  '&aacute;', '&acirc;',  
			'&agrave;', '&aring;',  '&atilde;', '&auml;',   '&ccedil;', '&eth;',    '&eacute;', 
			'&ecirc;',  '&egrave;', '&euml;',   '&iacute;', '&icirc;',  '&igrave;', '&iuml;',   
			'&ntilde;', '&oacute;', '&ocirc;',  '&ograve;', '&oslash;', '&otilde;', '&ouml;',   
			'&thorn;',  '&uacute;', '&ucirc;',  '&ugrave;', '&uuml;',   '&yacute;', '&aacute;', 
			'&acirc;',  '&aelig;',  '&agrave;', '&aring;',  '&atilde;', '&auml;',   '&ccedil;', 
			'&eacute;', '&ecirc;',  '&egrave;', '&eth;',    '&euml;',   '&iacute;', '&icirc;',  
			'&igrave;', '&iuml;',   '&ntilde;', '&oacute;', '&ocirc;',  '&ograve;', '&oslash;', 
			'&otilde;', '&ouml;',   '&szlig;',  '&thorn;',  '&uacute;', '&ucirc;',  '&ugrave;', 
			'&uuml;',   '&yacute;', '&yuml;',   '&iexcl;',  '&pound;',  '&curren;', '&yen;',    
			'&brvbar;', '&sect;',   '&uml;',    '&copy;',   '&ordf;',   '&laquo;',  '&not;',    
			'&shy;',    '&reg;',    '&macr;',   '&deg;',    '&plusmn;', '&sup2;',   '&sup3;',   
			'&acute;',  '&micro;',  '&para;',   '&middot;', '&cedil;',  '&sup1;',   '&ordm;',   
			'&raquo;',  '&frac14;', '&frac12;', '&frac34;', '&iquest;', '&times;',  '&divide;', 
			'&cent;',   '...',      '&micro;');

	if (validateaString($string, false)) {
		return str_replace($symbols, $entities, $string);
	} else {
		return $string;
	}
}
?>
